package com.hao.tx.dao.impl;

import com.hao.tx.dao.AccountDAO;
import com.hao.tx.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * TODO
 *
 * @author 97472
 * @version 1.0
 * @date 2021/11/10 15:07
 */
@Repository
public class AccountDAOImpl implements AccountDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void addMoney() {
        String querySql = "select * from s_account where id = ?";
        Account account = jdbcTemplate.queryForObject(querySql, new BeanPropertyRowMapper<Account>(Account.class), 1);
        String updateSql = "update s_account set balance = ? where id = ?";
        jdbcTemplate.update(updateSql,account.getBalance().add(new BigDecimal(100)),account.getId());
    }

    public void cutMoney() {
        String querySql = "select * from s_account where id = ?";
        Account account = jdbcTemplate.queryForObject(querySql, new BeanPropertyRowMapper<Account>(Account.class), 2);
        String updateSql = "update s_account set balance = ? where id = ?";
        jdbcTemplate.update(updateSql,account.getBalance().subtract(new BigDecimal(100)),account.getId());
    }
}
